Page({
    data: {
        is_video: false,
        video_direction: 0,
        is_webView: false,
        video_poster: '',
        video_source: '',
        artId: '',
        artName: '',
        publishDate: '',
        source: '',
        description:'',
        like: '',
        notLike: '',
        zanImage: '',
        caiImage: ''
    },
    onLoad: function (options) {
        let self = this
        let data = decodeURIComponent(options.news)
        var news = JSON.parse(data)
        var contents = news.contents
        // let url = 'https://v.qq.com/x/page/w097555m2yi.html' //腾讯视频地址 请替换
        let url = contents;

        this.setData({
            artId: news.artID,
            artName: news.artName,
            publishDate: news.publishDate,
            source: news.source,
            description: news.description,
            like: news.myLike,
            notLike: news.myUnLike
        })

        if(this.data.like == 1){
            this.setData({
              zanImage:'/image/zan@select.png'
            })
          }else{
            this.setData({
              zanImage:'/image/zan.png'
            })
          }
          if(this.data.notLike == 1){
            this.setData({
              caiImage:'/image/cai@select.png'
            })
          }else{
            this.setData({
              caiImage:'/image/cai.png'
            })
          }

        wx.showLoading({
            title: '加载中...'
        })
        if (url.indexOf('v.qq.com') !== -1) {
            var key = url.match(/http(s?):\/\/(.+?)\/(\w+?).html/)
            console.log(key)
            if (key && key[3]) {
                wx.request({
                    url: 'https://h5vv.video.qq.com/getinfo?otype=json&platform=11001&sphttps=1&vid=' + key[3],
                    success: function (res) {
                        var data = res.data
                        data = data.slice(13, data.length - 1)
                        try {
                            var ret = JSON.parse(data)
                            console.log(ret)
                            self.setData({
                                is_video: true,
                                video_source: ret.vl.vi[0].ul.ui[0].url + ret.vl.vi[0].fn + '?vkey=' + ret.vl.vi[0].fvkey,
                                video_poster: 'https://puui.qpic.cn/qqvideo_ori/0/' + key[3] + '_496_280/0',
                                video_direction: 90
                            })
                            // wx.setNavigationBarTitle({
                            //     title: ret.vl.vi[0].ti
                            // })
                        } catch (e) {
                            wx.showToast({
                                title: '视频地址解析失败',
                                icon: 'none',
                                duration: 2000
                            })
                        }
                    }
                })
            }
        } else {
            this.setData({
                is_webView: true,
                url: url
            })
        }
        setTimeout(function () {
            wx.hideLoading()
        }, 1500)
    },
    //点赞
  like: function () {
    var that = this
    var user = wx.getStorageSync("user")
    var userId = user.userID
    var artId = that.data.artId
    if (that.data.notLike) {
      wx.showToast({
        title: '您已踩过',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if (user !== '' && user !== null & user !== undefined) {
      if (that.data.like == 1) {
        this.removeLikeOrUnLike(artId, 1, userId)
      } else {
        this.addLikeOrUnLike(artId, 1, userId)
      }
    } else {
      wx.showToast({
        icon:'none',
        title: '请先登录!',
        duration: 2000
      })
    }

  },
  //踩
  notLike: function () {
    var that = this
    var user = wx.getStorageSync("user")
    var userId = user.userID
    var artId = that.data.artId

    if (that.data.like) {
      wx.showToast({
        title: '您已点过赞',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if (user !== '' && user !== null & user !== undefined) {
      if (that.data.notLike == 1) {
        this.removeLikeOrUnLike(artId, 0, userId)
      } else {
        this.addLikeOrUnLike(artId, 0, userId)
      }
    } else {
      wx.showToast({
        icon:'none',
        title: '请先登录!',
        duration: 2000
              
      })
    }

  },
  //点赞、踩
  addLikeOrUnLike(artId, isLike, userId) {
    var that = this
    wx.request({
      url: getApp().globalData.myHttp + '/news/addLikeOrUnLike?artID='+artId+"&isLike="+isLike+"&userID="+userId,
      method: "POST",
      header: {
        'content-type': 'application/json',
      },
      success(res) {
        if (res.data.success) {
          if(isLike == 1){
            that.setData({
              like: 1,
              zanImage:'/image/zan@select.png'
            })
          }else{
            that.setData({
              notLike: 1,
              caiImage:'/image/cai@select.png'
            })
          }
        }
      }
    });
  },
  //取消点赞、踩
  removeLikeOrUnLike(artId, isLike, userId) {
    var that = this
    wx.request({
      url: getApp().globalData.myHttp + '/news/removeLikeOrUnLike?artID='+artId+"&isLike="+isLike+"&userID="+userId,
      method: "POST",
      header: {
        'content-type': 'application/json',
      },
      success(res) {
        if (res.data.success) {
          if(isLike == 1){
            that.setData({
              like: 0,
              zanImage:'/image/zan.png'
            })
          }else{
            that.setData({
              notLike: 0,
              caiImage:'/image/cai.png'
            })
          }
        }
      }
    });
  },
})